package com.whcoding.mybatis.plus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whcoding.mybatis.plus.entity.OrmDeptVO;
import com.whcoding.mybatis.plus.service.OrmDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: spring-boot-learning
 * @description:
 * @author: whcoding
 * @create: 2022-08-10 16:09
 **/
@RequestMapping("mybatis")
@RestController
public class MyBastisPlusController {

	@Autowired
	OrmDeptService ormDeptService;

	/**
	 * 1.保存数据
	 */
	@PostMapping("/saveDept")
	public boolean saveDept() {
		return ormDeptService.save(getOrmDept());
	}

	/**
	 * 1.1 批量保存数据
	 */
	@PostMapping("/saveBatch")
	public boolean saveBatch() {
		List<OrmDeptVO> deptVOS = ormDeptService.list();
		deptVOS.forEach(c -> c.setRemark("批量保存数据"));
		return ormDeptService.saveBatch(deptVOS, deptVOS.size());
	}

	/**
	 * 2. 根据ID删除数据
	 *
	 * @return
	 */
	@DeleteMapping("/delete")
	public boolean delete() {
		return ormDeptService.removeById(1);
	}

	/**
	 * 3.更新数据
	 *
	 * @param deptNo
	 * @param id
	 * @return
	 */
	@PostMapping("/update")
	public boolean update(@RequestParam String deptNo, @RequestParam Long id) {
		OrmDeptVO deptVO = new OrmDeptVO();
		deptVO.setId(id);
		deptVO.setDeptNo(deptNo);
		return ormDeptService.updateById(deptVO);
	}

	/**
	 * 4.查询全部数据
	 *
	 * @return
	 */
	@GetMapping("/list")
	public List<OrmDeptVO> list() {
		return ormDeptService.list();
	}

	/**
	 * 5.分页查询
	 *
	 * @param current
	 * @param size
	 * @return
	 */
	@GetMapping("/page")
	public Page page(@RequestParam int current, @RequestParam int size) {
		return (Page) ormDeptService.page(new Page<>(current, size), new QueryWrapper(new OrmDeptVO()));
	}

	/**
	 * 构造数据
	 *
	 * @return
	 */
	private OrmDeptVO getOrmDept() {
		OrmDeptVO deptVO = new OrmDeptVO();
		deptVO.setDeptNo("2001");
		deptVO.setDeptName("运维部门");
		return deptVO;
	}
}
